﻿#encoding: utf-8
=begin

Usage:      

整理keyword
1)keyword:
将keyword['security']迁移到security
如果keyword['security']在合规列表内，则覆盖security
如果keyword['security']不在合规类表内，且security在合规列表范围内，则用security覆盖keyword['security']
2)tags:如果非法则重置为空
3)将keyword里面的tags开始转移到tags里

arguments:  ruby update-tags.rb

=end

load 'rds_api.rb'
load 'crmeb_utils.rb'

def get_products_ conditions
  products = []
  inq = "select id, source_pid, security, source_mid, sales, price, keyword, store_name, store_info, stock, is_show, cate_id, sort, temp_id, mer_id from crmeb.eb_store_product "
  inq += conditions.empty? ? 'where is_del = 0;' : " #{conditions};"
  res = queryRds inq
  res.each { |product| products += [product] }
  return products
end

def update_security_for products

  idx = 0
  products.each do |product|

    update_needed = false

    begin
      keyword = JSON.parse product['keyword']
    rescue => e
      puts "ERROR#1: keyword invalid for #{product['id']}\n"
      keyword = {'security':'其它安全标准','tags':[]}
      update_needed = true
    end
    
    if keyword.class != Hash
      puts "ERROR#2: keyword invalid for #{product['id']}\n"
      keyword = {'security':'其它安全标准','tags':[]}
      update_needed = true
    end


    # 修正错误的tags
    tags = keyword['tags']
    tags = [] if tags.nil?
    if tags.size > 0 && tags[0].include?('{')
      puts "ERROR#3: tags invalid for #{product['id']}: #{tags.join(' ')}\n"
      tags = []
      keyword.store('tags',tags)
      update_needed = true
    end

    keyword_security = keyword['security']
    product_security = product['security']

    security = '其它安全标准'

    valid_standards = "丰巢生态保证/中国有机认证/其它有机认证/零农残无激抗/不含负面添加".split("/");
    #如果都有安全标准数据，security优先覆盖keyword
    security = keyword_security if valid_standards.include? keyword_security
    security = product_security if valid_standards.include? product_security

    keyword.store('security', security)

    update_needed = true if security != product_security || security != keyword_security || product_security != keyword_security
    if update_needed 
      sql = "update crmeb.eb_store_product set security='#{security}' ,keyword='#{@rds.escape keyword.to_json}' where id = #{product['id']}"
      queryRds sql 
      print "o"
    else
      print "."
    end

    idx += 1 

  end

  puts "\ndone. #{idx}"

end

def migrate_tags_for products

  idx = 0
  products.each do |product|
    begin
      keyword = JSON.parse product['keyword']
    rescue => e
      keyword = {'security':'其它安全标准','tags':[]}
      update_needed = true
    end
    
    if keyword.class != Hash
      puts "ERROR#2: keyword invalid for #{product['id']}\n"
      keyword = {'security':'其它安全标准','tags':[]}
      update_needed = true
    end

    keyword_tags = keyword['tags']

    tags_string = ""
    keyword_tags.each do |tag|

      next if tag == ''
      next if tag.include? 'p#'
      next if tag.include? '300'
      next if tag.include? '您'
      next if tag.include? '你'
      next if tag.include? '10'
      next if tag.include? 'shipping'
      next if tag.include? '负面添'
      next if tag.include? '标准'
      next if tag.include? "好用"
      next if tag.include? "好快"
      next if tag.include? "测试"
      next if tag.include? "好吃"
      next if tag.include? "好看"
      next if tag.include? "好玩儿"
      next if tag.include? "巢生态"
      next if tag.include? "生态种植"
      next if tag.include? "安全标准"
      next if tag.include? "生态保证"
      next if tag.include? "主要省份"
      next if tag.include? "生态四无"
      next if tag.include? "有机认证"
      next if tag.include? "零农残"

      #用空格做分割符有好处，空值不会出现
      tag = tag.gsub('意大利无防腐剂系列护','意大利 无防腐剂')
      tag = tag.gsub('[',' ')
      tag = tag.gsub(']',' ')
      tag = tag.gsub('，',' ')
      tag = tag.gsub('　',' ')
      tag = tag.gsub('。',' ')
      tag = tag.gsub('、',' ')
      tag = tag.gsub('【',' ')
      tag = tag.gsub('+',' ')

      tags_string += " #{tag}"
      
    end
    
    #通过这个操作，把多余的空格滤掉
    tags = tags_string.split(' ')
    tags_string = tags.join(' ')

    #keyword.store("tags",[]) #清空
    keyword.store("tags",tags)  #暂时保留兼容性
    sql = "update crmeb.eb_store_product set tags='#{@rds.escape tags_string}' ,keyword='#{@rds.escape keyword.to_json}' where id = #{product['id']}"
    queryRds sql

    idx += 1

  end
  puts "\ndone. #{idx}"

end

puts "update security for products..."
products = get_products_ " " #一点条件都不
#update_security_for products
migrate_tags_for products
